package com.xxxx.server.utils;

import cn.hutool.core.date.DateUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import com.xxxx.server.pojo.EmployeeDataBase;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.xml.crypto.Data;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.Properties;

/**
 * @author dsr
 * @since 2020-10-15
 */
public class SendMailUtil {
    //邮件服务器主机名
    // QQ邮箱的 SMTP 服务器地址为: smtp.qq.com
    private static String myEmailSMTPHost = "smtp.qq.com";

    //发件人邮箱
    private static String myEmailAccount = "1561488897@qq.com";

    //发件人邮箱密码（授权码）
    //在开启SMTP服务时会获取到一个授权码，把授权码填在这里
    private static String myEmailPassword = "rpdvcfovghyvhchh";

    private  static boolean sendEmail(String toEmailAddress, String emailTitle, String emailContent){
        try {
            Properties props = new Properties();
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 端口号
            props.put("mail.smtp.port", 465);
            // 设置邮件服务器主机名
            props.setProperty("mail.smtp.host", myEmailSMTPHost);
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
            /**SSL认证，注意腾讯邮箱是基于SSL加密的，所以需要开启才可以使用**/
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            //设置是否使用ssl安全连接（一般都使用）
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            //创建会话
            Session session = Session.getInstance(props);
            //获取邮件对象
            //发送的消息，基于观察者模式进行设计的
            Message msg = new MimeMessage(session);
            //设置邮件标题
            msg.setSubject(emailTitle);
            //设置邮件内容
            msg.setContent(emailContent,"text/html;charset=utf-8");
            //设置发件人邮箱
            // InternetAddress 的三个参数分别为: 发件人邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码
            msg.setFrom(new InternetAddress(myEmailAccount,"系统管理员(xxx)", "UTF-8"));
            //得到邮差对象
            Transport transport = session.getTransport();
            //连接自己的邮箱账户
            //密码不是自己QQ邮箱的密码，而是在开启SMTP服务时所获取到的授权码
            //connect(host, user, password)
            transport.connect( myEmailSMTPHost, myEmailAccount, myEmailPassword);
            //发送邮件
            transport.sendMessage(msg, new Address[] { new InternetAddress(toEmailAddress) });
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean sendEmail(String email,EmployeeDataBase employeeDataBase){
        TemplateEngine templateEngine = SpringContextUtil.getBean("templateEngine");
        Context context = new Context();
        context.setVariable("emp",employeeDataBase);
         context.setVariable("time", DateUtil.now());
        String emailContent = templateEngine.process("emailTeplate", context);
       return  SendMailUtil.sendEmail(email,"祝贺你成为公司的一员",emailContent);

    }

}
